clear
%oad ../parameter_sandbox_ces/eq_mit_mass.mat 
x = load('../no_entry_or_exit/eq_mit_tfp.mat');
%load ../prod/calibration_v3/sandbox_ces/eq_mit_mass.mat
eq_mit_ces = x.eq_mit;

%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
y = load('../free_entry_labor/eq_mit_tfp.mat');
%load ../prod/calibration_v3/sandbox/eq_mit_mass.mat
eq_mit = y.eq_mit;
glob   = y.glob;
options = y.options;

%% get actual data - mass of producers
prods = repmat(exp(glob.sf(:,2)), 1, options.T);
 

Ns = 1 - 8/100;

Labor       =  eq_mit.l;
total_labor =  sum(eq_mit.mu.*Labor);

adj = total_labor/total_labor(end);
scx = pick_scale(adj, Ns);


%% pick scaling coefficients
Labor       =  eq_mit.l;
total_labor =  sum(eq_mit.mu.*Labor);

Ns      = log_linear_scale(eq_mit.A, scx);
adj_ces = eq_mit_ces.A;

scx_ces = pick_scale(adj_ces, Ns);
%scx_ces = [0 -diff(Ns-1)]/scx;

mass = sum(eq_mit.mu,1);


%% compute cost-weighted markups
ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);
labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = (labor_bill./ngdp);

%
costs = eq_mit_ces.l;
tfps  = repmat(eq_mit_ces.A', x.glob.Nsf, 1);
prods = tfps.*repmat(exp(x.glob.sf(:,2)), 1, x.options.T);
wages = repmat(eq_mit_ces.W', x.glob.Nsf, 1);
mkps  = eq_mit_ces.p./(wages./prods);
        
mkps_cw = sum(mkps.*costs.*eq_mit_ces.mu)./sum(costs.*eq_mit_ces.mu);
labor_share_ces = 1./mkps_cw;

% alt for baselinwe
costs = eq_mit.l;
tfps  = repmat(eq_mit.A', y.glob.Nsf, 1);
prods = tfps.*repmat(exp(y.glob.sf(:,2)), 1, y.options.T);
wages = repmat(eq_mit.W', y.glob.Nsf, 1);
mkps  = eq_mit.p./(wages./prods);
        
mkps_cw = sum(mkps.*costs.*eq_mit.mu)./sum(costs.*eq_mit.mu);
labor_share1 = 1./mkps_cw;


%%
set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')


close all

T = 15;
subplot(2,3,1)
mass = sum(eq_mit.mu,1);
plot(log_linear_scale_plot(mass, scx), 'LineWidth', 4); xlim([1 T]); title('Mass of producers')
ytickformat('percentage')

hold on
mass_ces = sum(eq_mit_ces.mu,1);
plot(log_linear_scale_plot(mass_ces, scx_ces), 'r-.', 'LineWidth', 4);
yline(0, '--')

%plot(Ns, 'LineWidth', 4)
legend('Baseline', 'No Entry or exit', 'Location', 'Southeast')

subplot(2,3,5)
plot(log_linear_scale_plot(eq_mit.C, scx), 'LineWidth', 4); xlim([1 T]);  title('Output')
hold on
plot(log_linear_scale_plot(eq_mit_ces.C, scx_ces), 'r-.', 'LineWidth', 4); xlim([1 T]);  title('Output')
ytickformat('percentage')

subplot(2,3,6)
plot(log_linear_scale_plot(eq_mit.W, scx), 'LineWidth', 4); xlim([1 T]);  title('Wage')
hold on
plot(log_linear_scale_plot(eq_mit_ces.W, scx_ces),  'r-.', 'LineWidth', 4)
ytickformat('percentage')

subplot(2,3,4)
plot(log_linear_scale_plot(eq_mit.L, scx), 'LineWidth', 4); xlim([1 T]); title('Employment')
hold on
plot(log_linear_scale_plot(eq_mit_ces.L, scx_ces), 'r-.', 'LineWidth', 4); xlim([1 T]); title('Employment')
ytickformat('percentage')

labor_share = 1./labor_share;
labor_share_ces = 1./labor_share_ces;

 subplot(2,3,2)
 plot((log_linear_scale_plot(labor_share, scx)), 'LineWidth', 4); xlim([1 T]); title('Markup')
 hold on
plot((log_linear_scale_plot(labor_share_ces, scx_ces)), 'r-.', 'LineWidth', 4); xlim([1 T]); title('Markup')
ytickformat('percentage')

subplot(2,3,3)

Z     = eq_mit.C./eq_mit.L';
tfp   = log_linear_scale(Z, scx);

Z     = eq_mit_ces.C./eq_mit_ces.L';
tfp_ces   = log_linear_scale(Z, scx_ces);

exog_tfp = log_linear_scale_plot(x.sim.A, scx);


plot(100*(tfp/tfp(end)-1), 'LineWidth', 4); xlim([1 T]);
hold on
plot(100*(tfp_ces/tfp_ces(end)-1),  'r-.', 'LineWidth', 4); 
xlim([1 T]);
ytickformat('percentage')

exog_tfp = plot(exog_tfp, 'k--', 'LineWidth', 4);

legend('', '', 'Exogenous TFP', 'Location', 'Southeast')

title('Effective TFP')
set(gcf,'units','points','position',[10,10,1000,600])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'figures/tfp_shock_role_of_entry_labor.png')



